/**
 * Knight.
 * 
 * @author Brian Boadi, Antoine Domenger
 * @version 1.0
 */

package lp.chess;

public class Knight extends Piece {
	//- CONSTRUCTOR(S) ------------------------------------------------------------------------

	/**
 	 * Default constructor.
 	 * Initializes an empty knight.
 	 */
	public Knight() {
		super();
	}

	/**
	 * Initializes a new knight of a given color.
	 * 
	 * @param white true if the knight must be white, false if it must be black.
	 */
	public Knight(boolean white) {
		super(white);
	}
	
	/**
	 * Initializes a new knight with all of its attributes.
	 * 
	 * @param x	the x coordinate of the knight on the chessboard.
	 * @param y the y coordinate of the knight on the chessboard.
	 * @param white true if the knight must be white, false if it must be black.
	 */
	public Knight(int x, int y, boolean white) {
		super(x, y, white);
	}
	
	/**
	 * Copy constructor.
	 * Initializes a new knight from a given one.
	 * 
	 * @param other the knight to copy in the new one.
	 */
	public Knight(Knight other) {
		super(other);
	}
	
	//-----------------------------------------------------------------------------------------

	//- OTHER METHOD(S) -----------------------------------------------------------------------

	/**
	 * Checks if a move is valid for this kind of piece.
	 * 
	 * @param cb the chessboard.
	 * @param x	the x coordinate where we want the knight to move on the chessboard.
	 * @param y the y coordinate where we want the knight to move on the chessboard.
	 * @param act true if we want to remove the piece that can be taken.
	 * 
	 * @return true if the move is valid.
	 */
	public boolean validMove(Chessboard cb, int x, int y, boolean act)
	{
		boolean isValid = (cb.isOutOfRange(x, y) && differentPosition(x, y));
		
		// Specific to this piece :
		if(isValid && !(
				(x == getX() + 2 && ((y == getY() + 1) || (y == getY() - 1))) ||
				(x == getX() - 2 && ((y == getY() + 1) || (y == getY() - 1))) ||
				(y == getY() + 2 && ((x == getX() + 1) || (x == getX() - 1))) ||
				(y == getY() - 2 && ((x == getX() + 1) || (x == getX() - 1)))
				)) {
			isValid = false;
		}
		
		if(isValid && cb.getPiece(x, y) != null && act)	// There's already a piece at this position, so it can be taken:
		{
			if(cb.getPiece(x, y).isWhite() != isWhite()) cb.removePiece(x, y);
			else isValid = false;
		}
		
		return isValid;
	}

	/**
	 * Returns the character associated to the type of this piece.
	 * 
	 * @return the character associated to the type of this piece 
	 */
	public char getType()
	{
		if(isWhite()) return 'N';
		else return 'n';
	}
	
	/**
	 * Returns a copy of this piece.
	 * 
	 * @return a copy of this piece.
	 */
	public Piece getCopy() {
		Knight copy = new Knight(m_x, m_y, m_white);
		
		return copy;
	}
	
	//-----------------------------------------------------------------------------------------
}
